package com.zxw.hotel.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zxw.hotel.common.Pageinfo;
import com.zxw.hotel.dao.UserDao;
import com.zxw.hotel.model.User;
import com.zxw.hotel.model.extend.UserExtend;
import com.zxw.hotel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private UserDao userDao;

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public List<User> getAll(Pageinfo pageinfo, UserExtend userExtend) {
        try (Page<?> __ = PageHelper.startPage(pageinfo.getPageNo(), pageinfo.getPageSize())) {
            return userDao.getAll(userExtend);
        }
    }

    @Override
    public int add(User user) {
        //判断userId userName sex password birthday 是否为空

        if ( user.getUserId().isEmpty() || user.getUserName().isEmpty() ||user.getSex()==null||
                user.getSex().isEmpty() || user.getPassword().isEmpty() ||user.getBirthday()==null) {
            return 0;

        }
        try{
            int add = userDao.add(user);
            return add;
        }catch (Exception e) {
            return 0;
        }

    }

    @Override
    public int update(User user) {
//判断userId userName sex password birthday 是否为空
        if ( user.getUserId().isEmpty() || user.getUserName().isEmpty() ||
                user.getSex().isEmpty() || user.getPassword().isEmpty()||user.getBirthday()==null) {
            return 0;

        }
        try {
            int update = userDao.update(user);
            return update;
        } catch (Exception e) {
            return 0;
        }

    }

    @Override
    public int delete(Integer[] ids) {
        return userDao.delete(ids);
    }
}
